<template>
    <div class="right-content">
      <!-- 头部查询区域 -->
        <el-form :inline="true" :model="dataForm2" class="demo-form-inline">
          <div style="display:inline-block">
            <!-- <el-form-item>
              <el-select v-model="dataForm2.region" placeholder="请选择" style="width:120px">
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item> -->
            <el-form-item>
              <el-input v-model="dataForm2.mtrId" id="minput" placeholder="请输入会议室编号"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchDataList()">查 询</el-button>
              <el-button @click="reset()">重 置</el-button>
              <el-button type="primary" @click="handleCreate()">新增会议室</el-button>
            </el-form-item>
          </div>
        </el-form>
        <!-- 内容区域 -->
        <el-table
          ref="MExamineDataList"
          border
          :data="MExamineDataList"
          :header-cell-style="headerStyle"
          :cell-style="cellStyle"
        >
          <!-- 复选框 -->
          <!-- <el-table-column
            type="selection"
            width="50"
            header-align="center"
            align="center">
          </el-table-column> -->
          <el-table-column
            type="index"
           :index="getIndex"
            width="80"
            label="序号"
            header-align="center"
            align="center">
          </el-table-column>
          <el-table-column
            width="80"
            label="id"
            prop="id"
            header-align="center"
            align="center"
            v-if="flag === 1">
          </el-table-column>
          <el-table-column
            prop="mtrId"
            width="252"
            label="会议室编号"
            header-align="center"
            align="center"
            >
            </el-table-column>
          <el-table-column
            prop="mtrName"
            width="260"
            label="会议室名称"
            header-align="center"
            align="center">
          </el-table-column>
          <el-table-column
            prop="mtrPos"
            width="260"
            label="会议室地点"
            header-align="center"
            align="center">
          </el-table-column>
          <el-table-column
            prop="mtrState"
            width="260"
            label="会议室状态"
            header-align="center"
            align="center">
          </el-table-column>
          <el-table-column
            fixed="right"
            width="180"
            header-align="center"
            align="center"
            label="操作">
            <template slot-scope="scope">
              <!-- <el-button size="small" type="success" plain @click="edit(scope.row)">同 意</el-button>
              <el-button size="small" type="danger" plain @click="removeById(scope.row.goods_id)">拒 绝</el-button> -->
              <!-- <el-button size="small" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)"></el-button> -->
              <el-button size="small" type="primary" style="width:20;height: 30px;" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="small" type="danger" style="width:20;height: 30px;" @click="handleDelete(scope.row)">删除</el-button>
              <!-- <el-link type="primary" @click="handleUpdate(scope.row)">修改</el-link>
              <el-link type="danger" @click="handleDelete(scope.row)">删除</el-link> -->
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <div class="block">
          <el-pagination
            align="right"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-size="pageSize"
            layout="total, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
        <!-- 弹窗，新增或修改 -->
        <el-dialog
              :title="textMap[dialogStatus]"
              :close-on-click-modal=true
              :visible.sync="dialogFormVisible"
              >
              <el-form
              :rules="rules"
              class="Addform"
              :model="dataForm"
              ref="dataForm"
              @keyup.enter.native="dataFormSubmit()"
              label-width="110px"
              label-position="left"
              style="width: 500px; margin-left: 80px">
                <div class="right">
                  <el-row :gutter="15">
                      <el-form-item label="会议室名称" style="width: 370px;" prop="mtrName" >
                        <el-input v-model="dataForm.mtrName" ></el-input>
                      </el-form-item>
                      <el-form-item label="会议室地点" style="width: 370px;" prop="mtrPos">
                        <el-input v-model="dataForm.mtrPos" ></el-input>
                      </el-form-item>
                      <el-form-item label="会议室状态" style="width: 370px;" prop="mtrState">
                          <el-select v-model="dataForm.mtrState" style="width: 260px;" placeholder="请选择会议室状态">
                            <el-option label="停用" value="停用"></el-option>
                            <el-option label="空闲" value="空闲"></el-option>
                            <el-option label="已被预约" value="已被预约"></el-option>
                            <el-option label="正在使用" value="正在使用"></el-option>
                          </el-select>
                      </el-form-item>
                  </el-row>
                </div>
              </el-form>
              <span slot="footer" class="dialog-footer">
                <el-button type="primary" @click="dialogStatus === 'create' ? createData() : updateData()">确 定</el-button>
                <el-button type="primary" @click="dialogFormVisible = false">取 消</el-button>
              </span>
        </el-dialog>
    </div>
</template>

<script>
import axios from 'axios'

export default {
  name: 'MeetingRoomUse',
  data() {
    return {
      options: [{
        value: '会议室编号',
        label: '会议室编号'
      }, {
        value: '会议室名称',
        label: '会议室名称'
      }, {
        value: '会议室地点',
        label: '会议室地点'
      }, {
        value: '会议室状态',
        label: '会议室状态'
      }
      ],
      value: '',
      // *******翻页***********
      MExamineDataList: [], // 流程定义文件数据
      currentPage: 1, // 当前页码
      pageSize: 5, // 每页选定的显示条数
      total: 0,
      // **********************
      dataForm2: {}, // 想要查询的设备数据
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      dialogFormVisible: false,
      dataForm: {
        mtrState: '空闲'
      },
      postId: 0,
      rules: {
        mtrName: [
          { required: true, message: '请输入会议室名称', trigger: 'blur' }
        ],
        mtrPos: [
          { required: true, message: '请输入会议室地点', trigger: 'blur' }
        ]
        // mtrState: [
        //   { required: true, message: '请输入会议室状态', trigger: 'blur' }
        // ]
      },
      flag: 0
    }
  },
  // 生命周期
  mounted() {
    this.getDataList()
  },
  // 页码变化
  // const service = axios.create({
  //   baseURL:'localhost:8080'
  // }),
  watch: {
    tableData(newValue, oldValue) {
      this.currentData = newValue.slice((this.currentPage - 1) * this.pageSize, this.currentPage * this.pageSize)
    },
    CurrentPage(newValue, oldValue) {
      this.currentPage = this.tableData.slice((newValue - 1) * this.pageSize, newValue * this.pageSize)
    }
  },
  methods: {
    reset() {
      this.getDataList()
      document.getElementById('minput').value = ''
    },
    // 表格行高
    cellStyle() {
      return {
        'font-size': '14px',
        height: '45px',
        padding: '0'
      }
    },
    // 表头行高
    headerStyle() {
      return {
        height: '55px'
      }
    },
    // ****************翻页*************
    // 改变当前页码
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    // 序号
    getIndex(index) {
      return index + (this.currentPage - 1) * this.pageSize + 1
    },
    // **********************************
    // 获取数据列表
    getDataList() {
      this.getRequest('/mtr' + '/' + this.currentPage + '/' + this.pageSize).then((resp) => {
        if (resp) {
          // console.log('-------------------------------------------')
          console.log(resp)
          this.MExamineDataList = resp.list
          this.total = resp.total
        } else {
          console.log('失败')
        }
      })
    },
    // 查询
    searchDataList(data) {
      this.dataLoading = true
      this.getRequest('/mtr/' + this.dataForm2.mtrId).then((resp) => {
        if (resp) {
          // console.log('-------------------------------------------')
          console.log(resp)
          this.MExamineDataList = resp.data
        } else {
          console.log('查询失败')
        }
      })
      this.dataLoading = false
      // this.getDataList()
    },
    // 新增
    handleCreate() {
      this.dialogFormVisible = true
      this.dialogStatus = 'create'
    },
    createData() {
      if (this.dataForm.mtrPos === undefined || this.dataForm.mtrName === undefined) {
        this.$message.error('请完成所有必填项')
      } else {
        console.log(this.dataForm.mtrState)
        this.postRequest('/mtr', {
          mtrName: this.dataForm.mtrName,
          mtrPos: this.dataForm.mtrPos,
          mtrState: this.dataForm.mtrState
        }).then((resp) => {
          if (resp) {
            console.log(resp)
            if (resp.msg === '新增成功') {
              this.$message.success(resp.msg)
            }
            this.dataForm = {}
            this.getDataList()
          } else {
            console.log('新增失败')
          }
        })
        this.getDataList()
        this.dialogFormVisible = false
        this.dataForm = {}
      }
    },
    // 编辑
    handleUpdate(data) {
      this.dialogFormVisible = true
      this.dialogStatus = 'update'
      this.postId = data.id
      this.dataForm = data
    },
    updateData() {
      this.putRequest('/mtr', {
        mtrName: this.dataForm.mtrName,
        mtrPos: this.dataForm.mtrPos,
        mtrState: this.dataForm.mtrState,
        mtrId: this.dataForm.mtrId
      }).then((resp) => {
        if (resp) {
          console.log(resp)
          this.$message.success(resp.msg)
          this.getDataList()
        } else {
          console.log('更新失败')
        }
      })
      this.getDataList()
      this.dialogFormVisible = false
      // this.getDataList()
    },
    // 删除
    handleDelete(data) {
      this.$confirm('此操作将永久删除【' + data.mtrId + '】会议室的所有信息，是否继续？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(() => {
        this.deleteRequest('/mtr/' + data.mtrId).then((resp) => {
          console.log(resp)
          this.$message.success(resp.msg)
          this.getDataList()
          // this.$forceUpdate()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
      this.getDataList()
    },
    onSubmit() {
      console.log('submit!')
    }
  }
}
</script>

<style scoped>
.right{
  height: 240px;
}
.demo-form-inline{
  width: 97%;
  display: flex;
  justify-content: space-between;
  margin: 20px auto;
  margin-bottom: 0px;
}
.el-table{
  width: 97%;
  margin: 0px auto
}
.el-dialog__body{
  height: 150px !important;
  overflow: auto;
}
/** 分页器 start  */
.block {
  position: absolute;
  top: 94% !important;
  right: 1.5%;
}
.el-pagination {
  align-items: center;
}
.el-pagination /deep/ span:not([class*=suffix]) {
  font: 1vw '宋体' !important;
  padding-top: 0px;
}
.el-pagination /deep/ .el-pagination__total {
  padding-top: 5px !important;
}
.el-pagination /deep/ .el-input__inner {
  font: 1vw '宋体' !important;
}
/** 分页器 end */
</style>
